import { JSEncrypt } from "JSEncrypt";
import smCrypto from "sm-crypto";

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const rsaPublicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";
const rsaPrivateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKNPuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gAkM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWowcSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99EcvDQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthhYhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3UP8iWi1Qw0Y=";

/** RSA */
// 加密
export function encrypt(txt: string) {
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(rsaPublicKey); // 设置公钥
    return encryptor.encrypt(txt); // 对数据进行加密
}

// 解密
export function decrypt(txt: string) {
    const encryptor = new JSEncrypt();
    encryptor.setPrivateKey(rsaPrivateKey); // 设置私钥
    return encryptor.decrypt(txt); // 对数据进行解密
}

/** 国密加解密工具类 */

const sm2 = smCrypto.sm2;
const cipherMode = 1; // 1 - C1C3C2，0 - C1C2C3，默认为1
const smPublicKey = "04298364ec840088475eae92a591e01284d1abefcda348b47eb324bb521bb03b0b2a5bc393f6b71dabb8f15c99a0050818b56b23f31743b93df9cf8948f15ddb54";

// SM2加密
export function doSm2Encrypt(msgString: string) {
    return sm2.doEncrypt(msgString, smPublicKey, cipherMode);
}
// SM2数组加密
export function doSm2ArrayEncrypt(msgString: any) {
    return sm2.doEncrypt(msgString, smPublicKey, cipherMode);
}
